#!/bin/sh

FORCE_CREATE_PATCH_FILE=""
if [ "$1" == "-f" -o "$1" == "--force" ]; then
	FORCE_CREATE_PATCH_FILE="-YES-"
	shift
fi

PATCH_FILE_TO_CREATE="$1"
shift

if [ "$PATCH_FILE_TO_CREATE" == "" ]; then
	echo "Usage: $0 [-f|--force] <patch file> <svn options>"
	echo "  <patch file> is the file to write the patch to."
	echo "  <svn options> will be passed to svn info and svn diff."
	echo "    It can contain the directory you want to create the patch for."
	echo "  The patch file will not be overwritten if it exists unless"
	echo "  the -f or --force option is passed."
	exit 1
fi

if [ -e "$PATCH_FILE_TO_CREATE" -a "$FORCE_CREATE_PATCH_FILE" == "" ]; then
	echo "$0: Can't overwrite file $PATCH_FILE_TO_CREATE, specify --force to ignore and overwrite."
	exit 1
fi

(touch "$PATCH_FILE_TO_CREATE" || echo "Cannot create $PATCH_FILE_TO_CREATE") || exit 1
svn info $@ > "$PATCH_FILE_TO_CREATE"
echo "# -*- *-* Patch below in svn diff format." >> "$PATCH_FILE_TO_CREATE"
svn diff $@ >> "$PATCH_FILE_TO_CREATE"

echo "File $PATCH_FILE_TO_CREATE created with patch."
